const BaseRest = require('./base.js');

const qiniu = require('qiniu');
const qiniuConfig = think.config('qiniu');
const accessKey = qiniuConfig['ak'];
const secretKey = qiniuConfig['sk'];
const bucket = qiniuConfig['bucket'];
const domain = qiniuConfig['domain'];
// const key = qiniuConfig['key'];

module.exports = class extends BaseRest {
  postAction() {
    const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
    const options = {
      scope: bucket // + ':'+key
    };
    const putPolicy = new qiniu.rs.PutPolicy(options);
    const uploadToken = putPolicy.uploadToken(mac);

    // think.logger.info(uploadToken);

    return this.success({ 'token': uploadToken, 'domain': domain });
  }
};
